package com.xwb.laboratory.test;

import java.util.concurrent.locks.ReentrantLock;

public class LockDemo {

	final ReentrantLock lock = new ReentrantLock();
	
	void test1() throws InterruptedException {
		lock.lock();
		
		Thread t = new Thread(() -> {
			try {
				lock.lockInterruptibly();
			} catch (InterruptedException e) {
				System.out.println("t 没抢到锁");
				e.printStackTrace();
			}
		});
		t.start();
		
		Thread.sleep(3000L);
		System.out.println("打断 t");
		t.interrupt();
		
		lock.unlock();
		
	}
	
	void test2(){
		lock.lock();
		System.out.println(lock.getHoldCount());
		
		new Thread(() -> {
			lock.lock();
			System.out.println("子线程抢到锁。。。");
		}).start();
		
		lock.lock();
		System.out.println(lock.getHoldCount());
		lock.unlock();
		System.out.println(lock.getHoldCount());
		lock.unlock();
		System.out.println(lock.getHoldCount());
		
		
	}
	
	public static void main(String[] args) throws InterruptedException {
		LockDemo ld = new LockDemo();
		ld.test2();
	}
}
